<?php 
namespace app\api\controller\Content;

use app\api\controller\Common;
use app\api\Logic\CommonLogic;
use app\api\Logic\Interact\InteractLogic;
use app\api\Logic\Notice\NoticeLogic;
use app\api\model\CommentModel;
use app\api\model\InteractModel;
use app\api\model\InteractThemeModel;

/**
 * 内容中心
 * 互动讨论
 */
class Interact extends Common{
    protected $noNeedLogin = ['*'];

    /**
     * 互动讨论主题
     */
    public function getThemeList(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);
            if(empty($param['type'])){
                $this->error('未传入类型');
            }
            $where = [
                'a.type' => 5,
                'a.shop_id' => ['in',[0,$param['shop_id']]]
            ];
            if($param['type'] == 2){
                //获取推荐主题
                $where['a.is_top'] = 1;
            }
            $themeModel = new InteractThemeModel();
            $list = $themeModel
            ->alias('a')
            ->where($where)
            ->join('interact b','a.id = b.theme_id','left')
            ->where('a.deletetime','null')
            ->field('a.id,a.title,a.content,a.image,count(b.id) as article_num')
            ->group('a.id')
            ->select();
            return $this->success('查询成功',$list);
        }
        $this->error('请求错误');
    }

    /**
     * 发帖子
     */
    public function release(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);
            if(empty($param['theme_id'])){
                $this->error('未传入主题id');
            }
            if(empty($param['title'])){
                $this->error('未传入标题');
            }
            if(empty($param['content'])){
                $this->error('未传入内容');
            }
            $images = !empty($param['images']) ? implode(',',$param['images']):'';
            $res = model('InteractModel')->allowField(true)->save([
                'shop_id' => $param['shop_id'],
                'title' => $param['title'],
                'theme_id' => $param['theme_id'],
                'content' => $param['content'],
                'images' => $images,
                'type' => 1,
                'relation_id' => $param['teacher_id'],
            ]);
            if($res){
                $this->success('发布成功');
            }
            $this->error('发布失败');
        }
        $this->error('请求错误');
    }

    /**
     * 获取贴子列表
     *
     */
    public function getInteractList()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);
            $theme_id = input('theme_id',0);
            $order_type = input('order_type',1);
            $list = $this->getList($param['shop_id'],$param['teacher_id'],$param['page'],$param['page_size'],$theme_id,$order_type);
            $this->success('获取成功',$list);
        }
        $this->error('请求错误');
    }

    /**
     * 我的发布
     *
     * @return void
     */
    public function myInteractList(){
        $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);
            $map['a.relation_id'] = $param['teacher_id'];
            $list = $this->getList($param['shop_id'],$param['teacher_id'],$param['page'],$param['page_size'],0,1,$map);
            $this->success('获取成功',$list);
    }

    public function getList($shop_id,$user_id,$page,$limit,$theme_id=0,$order_type=1,$map=[])
    {
        $commonLogic = new CommonLogic();
        $shop_arr = [0,$shop_id];
        $where = [
            'a.status'=>1,
            'a.deletetime' => null,
            'a.type'=>['in',[0,1]]
        ];
        if(empty($map)){
            $where['a.shop_id'] = ['in',$shop_arr];
        }
        if($theme_id){//是否主题列表
            $where['theme_id'] = $theme_id;
        }
        $order = '';
        switch ($order_type) { //排序类型:1=普通排序,2=热帖排序
            case '1':
                $order = 'a.is_top desc,a.shop_id asc,a.weigh desc,a.createtime desc';
                break;
            case '2':
                $order = 'a.views desc,a.share_num desc,a.like_num desc,a.createtime desc,a.weigh desc';
                break;
        }

        $interactModel = new InteractModel();
        $res = $interactModel->alias('a')
        ->with(['comment','theme'])
        ->where($where)
        ->where($map)
        ->join('comment b',"a.id = b.relation_id and b.style = 'Interact'",'left')
        ->join('likes c',"a.id = c.relation_id and c.style = 'Interact' and c.user_id='{$user_id}' ",'left')
        ->group('a.id')
        ->order($order)
        ->field('a.id,a.shop_id,a.title,a.theme_id,a.content,a.images,a.type,a.relation_id,a.views,a.share_num,a.like_num,a.createtime,count(distinct b.id) as comment_num,count(distinct c.id) as is_like')
        ->limit(($page-1)*$limit,$limit)
        ->select();
        if($res){
            foreach ($res as $v) {
                //获取作者头像信息
                $user_info = $commonLogic->getUserInfo($v['type'],$v['relation_id']);
                $v['nickname'] = $user_info['nickname'];
                $v['avatar'] = $user_info['avatar'];
                if($v['comment']){//存在评论 评论用户也加上昵称
                    foreach ($v['comment'] as $comment_v) {
                        $comment_user_info = $commonLogic->getUserInfo($comment_v['reply_type'],$comment_v['reply_id']);
                        $comment_v['nickname'] = $comment_user_info['nickname'];
                    }
                }   
            }
        }
        return $res;
    }

    /**
     * 获取贴子详情
     *
     */
    public function getInteractDetail()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);

            if(empty($param['relation_id'])){
                $this->error('未传入贴子id');
            }
            $interactModel = new InteractModel();
            $commonLogic = new CommonLogic();
            $where = [
                'a.status'=>1,
                'a.deletetime' => null,
                'a.id' => $param['relation_id'],
            ];
            $teacher_id = $param['teacher_id'];

            $data = $interactModel->alias('a')
            ->with(['theme'])
            ->where($where)
            ->join('comment b',"a.id = b.relation_id and b.style = 'Interact'",'left')
            ->join('likes c',"a.id = c.relation_id and c.style = 'Interact' and c.user_id='{$teacher_id}' ",'left')
            ->group('a.id')
            ->field('a.id,a.shop_id,a.title,a.theme_id,a.content,a.images,a.type,a.relation_id,a.views,a.share_num,a.like_num,a.createtime,count(distinct b.id) as comment_num,count(distinct c.id) as is_like')
            ->find();
            if($data){
                //获取作者头像信息
                $user_info = $commonLogic->getUserInfo($data['type'],$data['relation_id']);
    
                $data['nickname'] = $user_info['nickname'];
                $data['avatar'] = $user_info['avatar'];
                // 浏览数+1
                $data->setInc('views');
            }
            if(!$data){
                $this->error('贴子不存在或未审核');
            }
            $this->success('获取成功',$data);
        }
        $this->error('请求错误');
    }

    /**
     * 互动讨论贴子点赞
     *
     */
    public function clickLikes()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);

            if(empty($param['relation_id'])){
                $this->error('未传入贴子id');
            }
            $commonLogic = new CommonLogic();
            $res = $commonLogic->giveLikes($param['teacher_id'],$param['shop_id'],$param['relation_id'],'Interact','interact','like_num');
            if($res){
                $this->success('操作成功',['status'=>$res]);
            }
            $this->error('操作失败');
        }
        $this->error('请求错误');
    }

    /**
     * 贴子分享成功
     *
     */
    public function shareSuccess()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);

            if(empty($param['relation_id'])){
                $this->error('未传入贴子id');
            }
            $commonLogic = new CommonLogic();
            $res = $commonLogic->handleShare($param['teacher_id'],$param['shop_id'],$param['relation_id'],'Interact','interact','share_num');
            if($res){
                $this->success('操作成功');
            }
            $this->error('操作失败');
        }
        $this->error('请求错误');
    }

    /**
     * 评论提交
     *
     */
    public function releaseInteractComment()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);
            $to_id = input('to_id',0);
            if(empty($param['relation_id']) || empty($param['content']) ){
                $this->error('参数不完整');
            }
            $commonLogic = new CommonLogic();
            $res = $commonLogic->insertComment($param['shop_id'],'Interact',1,$param['relation_id'],$param['teacher_id'],$param['content'],$to_id);
            if($res){
                $this->success('发布成功');
            }
            $this->error('发布失败');
        }
        $this->error('请求错误');
    }

    /**
     * 获取评论列表
     *
     */
    public function interactCommentList()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);

            if(empty($param['relation_id'])){
                $this->error('未传入贴子id');
            }
            $commonLogic = new CommonLogic();
            $list = $commonLogic->getCommentList('Interact',$param['relation_id'],$param['page'],$param['page_size']);
            $where = ['a.style' => 'Interact','a.relation_id' => $param['relation_id'],'a.deletetime' => null];
            $count = model('CommentModel')->alias('a')->where($where)->count();
            $count = 1;

            $this->success('获取成功',['count'=>$count,'list'=>$list]);
        }
        $this->error('请求错误');
    }

    /**
     * 互动未读消息
     *
     * @return void
     */
    public function notice(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);
            $interact_where = [
                'a.style' => 'Interact',
                'a.to_reply_id' => $param['teacher_id'],
                'a.to_reply_type' => 1,
                'a.is_read' => 0,
                'a.deletetime' => null,
                'a.shop_id' => ['in',[0,$param['shop_id']]]
            ];
            $commentModel = new CommentModel();
            $notice = $commentModel->alias('a')->where($interact_where)->count();
            return $this->success('查询成功',['num'=>$notice]);
        }
        $this->error('请求错误');
    }

    /**
     * 获取互动消息列表
     *
     */
    public function getInteractNoticeList()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);

            $commentModel = new CommentModel();
            $commonLogic = new CommonLogic();
            $page = $param['page'];
            $limit = $param['page_size'];
            $interact_where = [
                'a.style'=>'Interact',
                'a.to_reply_id' => $param['teacher_id'],
                'a.to_reply_type' => 1,
                'a.is_read' => 0,
                'a.deletetime' => null,
                'a.shop_id' => ['in',[0,$param['shop_id']]]
            ];
            $interact = $commentModel
            ->alias('a')
            ->where($interact_where)
            ->join('interact c',"a.relation_id = c.id and a.style='Interact' ",'left')//互动讨论
            ->join('theme d',"c.theme_id = d.id and a.style='Interact' ",'left') //互动讨论主题
            ->join('comment e',"a.to_id = e.id",'left') //我发的评论
            ->order('a.createtime desc')
            ->field('a.id,a.shop_id,a.style,a.relation_id,a.reply_type,a.reply_id,a.content,a.createtime,a.style,c.title as interact_title,d.title as interact_theme,e.content as my_content')
            ->limit(($page-1)*$limit,$limit)
            ->select();
            foreach ($interact as $v) {
                switch ($v['style']) {
                    case 'Interact'://互动讨论：贴子标题、贴子主题
                        $v['title'] = $v['interact_title'];
                        $v['theme'] = $v['interact_theme'];
                        break;               
                }
                unset($v['interact_title']);
                unset($v['interact_theme']);
                $user_info = $commonLogic->getUserInfo($v['reply_type'],$v['reply_id']);
                $v['nickname'] = $user_info['nickname'];
                $v['avatar'] = $user_info['avatar'];
            }
    
            $interact = $interact?$interact:[];
            $this->success('查询成功',$interact);
        }
        $this->error('请求错误');
    }

    /**
     * 消息设为已读
     *
     * @return void
     */
    public function readNotice()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);
            if(!$param['ids']){
                $this->error('参数错误');
            }
            $noticeLogic = new NoticeLogic();
            $res = $noticeLogic->readNotice($param['ids'],$param['teacher_id'],'interact');
            if($res){
                $this->success('操作成功');
            }
            $this->success('操作失败');
        }
        $this->error('请求错误');
    }
}